/**
 * Created by Administrator on 2017/7/6.
 */
mui.init(
    {

    }
);

mui.plusReady(function(){
    console.log( _baseUrl);
    var user_id = plus.storage.getItem('cg_user_id'),
        user_token = plus.storage.getItem('cg_user_token'),
        wait = plus.nativeUI.showWaiting('正在加载中...'),
        toSign=plus.webview.currentWebview().toSign;// 判断是否从签到按钮进入本页面
    myAjax('exchange/exchangelist/index','post',{user_id:user_id,user_token:user_token,toSign:toSign},function(data){
        if(data.success){
            $('.banner>.score>span').html(data.data.now+'/'+data.data.score);
            console.log(JSON.stringify(data));
            console.log(data.sign);
            var list=data.data.product;
             addList(list);
            if(toSign){
                console.log(data.sign);
                console.log(JSON.stringify(data));
                if ( data.sign=='success'){
                    swal({
                        title: "今日签到成功！",
                        text: "签到成功，获得1积分",
                        type: "success",
                        confirmButtonColor:'#57B9F3'
                    });
                }else if(data.sign=="已签到"){
                    swal({  
                        title: "",
                        text: "今日已签到！",
                        confirmButtonColor:'#57B9F3'
                    });
                }else{
                    swal({
                        title: "",
                        text: "签到失败，请重新签到！",
                        confirmButtonColor:'#57B9F3'
                    });
                }
            }
            setTimeout(function(){
                wait.close();
            },1);
        }else{
            mui.alert("网络错误，请重新尝试");
        }
    });
    $(".new_lists").on('click','.new_list',function() {
        var detail_id = $(this).attr('data-id');
        console.log(detail_id+'列表的id');
        mui.openWindow({
            url: 'commodity.html',
            extras: {
                'detail_id': detail_id
            }
        })
    });
    $('.record').on('click',function(){
        mui.openWindow({
            url: 'record.html',
            extras: {
            }
        })
    })
});
function addList(data) {
    var html='';
    for(var i=0;i<data.length;i++){
        html+= '<div class="new_list" data-id="'+data[i].id+'">'+
                    '<div class="box">'+
                        '<div class="new_img" style="background:url('+_baseUrl+data[i].imgpath+') 50% 50% no-repeat;background-size: cover;"> <span class=""></span></div>'+
                        '<div class="new_title">'+data[i].title+'</div>'+
                        '<div class="new_msg">'+
                            '<span class="new_over">剩余'+data[i].left+'</span>';
                        if(data[i].left){
                            html+=  '<span class="new_score has">'+
                                '<span class="new_score ">'+data[i].price+'</span>'+
                                '</div>'+
                                '</div>';
                        }else{
                            html+=  '<span class="new_score hasnot">'+
                                '<span class="new_score ">'+data[i].price+'</span>'+
                                '</div>'+
                                '</div>'+
                                '<div  class="nomore"></div>'
                            ;

                        }

                 html+='</div>';
    }

    $('.new_lists').append(html);
    console.log('append')
};
